<?php
	
	namespace Aspic;
	
	use Aspic as A;
	
	/**
	* Standard class like PHP stdclass 
	* that allow convertion from stdclass or array
	* So, it should be data class, because convertFrom method
	* create an instance of StdData with no arguments
	*/
	Trait StdData {
		
		use StdTrait {
			__construct as stdTraitInit;
		}
		
		public function __construct($data = array(), $returnNullIfEmpty = true) {
			$this->stdTraitInit($data, $returnNullIfEmpty);
		}
		
		public function toStdclass() {
			$std = new \stdclass;
			
			foreach($this->_vars as $k => $val) {
				$std->{$k} = $val;
			}
			
			return $std;
		}
		
		public function toArray() {
			$arr = $this->getVars();
			
			return $arr;
		}
		
		public static function convertFrom($data) {
			$toConvert = $data;
		
			if($data instanceof \stdclass) {
				$toConvert = (array)$data;
			}
			elseif ($data instanceof StdData) {
				$toConvert = $data->getVars();
			}
			
			if(is_array($toConvert)) {
				$obj = new static;
			
				foreach($data as $k => $v) {
					$obj->{$k} = $v;
				}
				
				return $obj;
			}
			else {
				throw new \InvalidArgumentException('Convertion failed');
			}
		}
		
		public function dump() {
			var_dump($this->getVars());
		}
		
	}
	
?>